<?php
declare (strict_types=1);

namespace app\admin\model\data;

use app\common\model\DeviceType;
use think\model\concern\SoftDelete;

/**
 *
 */
class Student extends DataModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'user';

    /**
     * 设置隐藏字段
     *
     * @var array
     */
    protected $hidden = ['password', 'salt'];
    // 定义全局的查询范围
    protected $globalScope = ['mhm'];

    /**
     * 获取截止指定时间的用户注册量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     *
     * @param    [type]                         $time [description]
     * @return   [type]                               [description]
     */
    public static function getRegisterCountByTime($time = null)
    {
        $time = is_null($time) ? time() : $time;

        return Student::where('create_time', '<=', $time)->count();
    }

    /**
     * 获取一个时间段内的用户注册量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     *
     * @param array $timespan [description]
     * @return   [type]                                   [description]
     */
    public static function getRegisterCountByTimespan(array $timespan)
    {
        return Student::whereBetween('create_time', $timespan)->count();
    }

    /**
     * 来源
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     *
     * @return   [type]                         [description]
     */
    public function getRegisterDeviceTextAttr($value = '', $data)
    {

        $text          = DeviceType::getDeviceList();
        $text['admin'] = '后台添加';

        return $text[$data['register_device']] ?? '后台添加';
    }
}
